using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

using Leo.Core.Identity.Contracts;

using Volo.Abp.Modularity;
using Volo.Abp.Security.Claims;

namespace Leo.Core.Identity
{
    public static class IdentityServiceCollectionExtention
    {
        public static void SetupIdentity(this ServiceConfigurationContext context, bool isConfigIdentity = false)
        {
            var configuration = context.Services.GetConfiguration();
            context.Services.Configure<ConfigIdentityOptions>(configuration.GetSection(ConfigIdentityOptions.SectionName));

            if (isConfigIdentity)
            {
                context.Services.AddSingleton<ITokenProvider, JwtTokenProvider>();
            }
            context.Services.Replace(ServiceDescriptor.Singleton<ICurrentPrincipalAccessor, ConfigIdentityCurrentPrincipalAccessor>());
            context.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        }
        public static bool ReadIsConfigIdentity(this ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            context.Services.Configure<ConfigIdentityOptions>(configuration.GetSection(ConfigIdentityOptions.SectionName));

            return configuration[$"{ConfigIdentityOptions.SectionName}:IsEnable"] != null && bool.Parse(configuration[$"{ConfigIdentityOptions.SectionName}:IsEnable"]);
        }
    }
}
